package hs.framework.opcClient.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import hs.framework.opcClient.entity.SourceParamB;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;

public class ParamDataUtil {
    public static void main(String[] args) {
        String st = readFileContent("E:\\navicat\\Navicat Premium12-中文破解版\\sql\\楼控excel\\dataitems照明-2全部.json");
        JSONArray tableData = JSONArray.parseArray(st);
        getKeyJSONArray(tableData,"itemId","[Datapoints\\B1\\B1AEL7\\B1AEL7-7]");
//        getKeyJSONArray(tableData,"value","23100");
//        getKeyJSONArray(tableData,"itemId","[Datapoints\\a区3f-------5\\3AEL4-3F-A馆-1\\B1AEL6-8]");
//        getKeyJSONArray(tableData,"itemId",".LastError");
//        getKeyJSONArray(tableData,"itemId","DESC");
//        getAll(tableData);
//        selectKeyWord(tableData);
//          getAllLightControl(tableData);
    }

    public static HashMap<String,SourceParamB> getAllLightControl(JSONArray array){
        HashMap<String,SourceParamB> map = new HashMap<>();
        //第一遍遍历，找出所有设备名称
        for (int i = 0; i < array.size(); i++){
            JSONObject object = (JSONObject) array.get(i);
            String[] itemId = object.get("itemId").toString().split("[.]");
            if (itemId.length<2){
                System.out.println("itemId分割失败，请检查输入数据!");
                System.out.println("输入数据为："+object);
                System.out.println();
                continue;
            }
            String key = itemId[0];
            SourceParamB sourceParamB = new SourceParamB();
            if (map.get(key) == null) {
                sourceParamB.setCode(key);
                sourceParamB.setPointType(key);
            }else {
                sourceParamB = map.get(key);
            }
            if (itemId[1].equals("TriggerWrite")){
                sourceParamB.setTriggerWrite(object.get("value").toString());
            }
            else if (itemId[1].equals("TriggerRead")){
                sourceParamB.setTriggerRead(object.get("value").toString());
            }
            else if (itemId[1].equals("CanRead")){
                sourceParamB.setCanRead(object.get("value").toString());
            }
            else if (itemId[1].equals("CanWrite")){
                sourceParamB.setCanWrite(object.get("value").toString());
            }
            else if (itemId[1].equals("NominalValue")){
                sourceParamB.setNominalValue(object.get("value").toString());
            }
            else if (itemId[1].equals("LastUpdate")){
                Long timestamp = Long.valueOf(object.get("value").toString());
                LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timestamp/1000, 0, ZoneOffset.ofHours(8));
                sourceParamB.setDataUpdateTime(localDateTime);
            }
            else if (itemId[1].equals("ActualValue")){
                sourceParamB.setValueType(object.get("dataType").toString());
                sourceParamB.setCurrentValueName(object.get("itemId").toString());
                sourceParamB.setParamValue(object.get("value").toString());
                sourceParamB.setPointType(key);
            }
            else if (itemId[1].equals("Text")) {
                sourceParamB.setText(object.get("value").toString());
            }
            else if (itemId[1].equals("LastError")){
                sourceParamB.setLastError(object.get("value").toString());
            }
            sourceParamB.setEquipmentId(1L);
            map.put(key,sourceParamB);
        }

        for(Map.Entry<String,SourceParamB> entry : map.entrySet()){
            System.out.println(entry.getValue().toString());
        }
        System.out.println(map.size());
        return map;
    }

    public static Set<String> selectKeyWord(JSONArray array){
        Set<String> set = new HashSet<>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = (JSONObject) array.get(i);
            String str = object.get("itemId").toString().split("[.]")[1];
            if (set.add(str) == true){
                System.out.println(object);
            }
        }
        return set;
    }

    public static HashMap<String, SourceParamB> getAllBuildingControl(JSONArray array){
        HashMap<String,SourceParamB> map = new HashMap<>();
        //第一遍遍历，找出所有设备名称
        for (int i = 0; i < array.size(); i++){
            JSONObject object = (JSONObject) array.get(i);
            if (object.get("itemId").toString().indexOf("_SYSN") != -1){
                SourceParamB sourceParamB = new SourceParamB();
                //记录参数对应的设备
                String str = object.get("value").toString();
                sourceParamB.setPointType(str.substring(0,str.lastIndexOf("_")));
                //记录code
                sourceParamB.setCode(object.get("itemId").toString().replace("_SYSN",""));
                //设备id非空
                sourceParamB.setEquipmentId(1L);
                map.put(sourceParamB.getCode(),sourceParamB);
            }
        }

        for (int i = 0; i < array.size(); i++){
            JSONObject object = (JSONObject) array.get(i);
            //中文描述
            if (object.get("itemId").toString().indexOf("_DESC") != -1){
                String key = object.get("itemId").toString().replace("_DESC","");
                SourceParamB sourceParamB = map.get(key);
                sourceParamB.setName(object.get("value").toString());
                //中文拼音缩写
                sourceParamB.setParamType(ChineseCharToEnUtil.getFirstSpell(sourceParamB.getName()));
                map.put(sourceParamB.getCode(),sourceParamB);
            }

                //记录实时值参数
            else if (object.get("itemId").toString().indexOf("_CV") != -1){
                String key = object.get("itemId").toString().replace("_CV","");
                SourceParamB sourceParamB = map.get(key);
                //实时值参数
                sourceParamB.setCurrentValueName(object.get("itemId").toString());
                //实时值
                sourceParamB.setParamValue(object.get("value").toString());
                //实时值参数类型
                sourceParamB.setValueType(object.get("dataType").toString());
                map.put(sourceParamB.getCode(),sourceParamB);
            }

            //最大值
            else if (object.get("itemId").toString().indexOf("_ALHI") != -1){
                String key = object.get("itemId").toString().replace("_ALHI","");
                SourceParamB sourceParamB = map.get(key);
                sourceParamB.setParamMax(object.get("value").toString());
                map.put(sourceParamB.getCode(),sourceParamB);
            }

            //最小值
            if (object.get("itemId").toString().indexOf("_ALLO") != -1){
                String key = object.get("itemId").toString().replace("_ALLO","");
                SourceParamB sourceParamB = map.get(key);
                sourceParamB.setParamMin(object.get("value").toString());
                map.put(sourceParamB.getCode(),sourceParamB);
            }
        }


        for(Map.Entry<String,SourceParamB> entry : map.entrySet()){
            System.out.println(entry.getValue().toString());
        }
        System.out.println(map.size());
        return map;
    }


    public static JSONArray getKeyJSONArray(JSONArray array,String key,String value){
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < array.size(); i++){
            JSONObject object = (JSONObject) array.get(i);
            if (object.get(key).toString().indexOf(value) != -1){
                jsonArray.add(object);
                System.out.println(object);
            }
        }
        System.out.println(jsonArray.size());
        System.out.println(jsonArray);
        return jsonArray;
    }

    public static JSONArray getWordJSONArray(JSONArray array,String key){
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < array.size(); i++){
            JSONObject object = (JSONObject) array.get(i);
            if (object.toString().indexOf(key) != -1){
                jsonArray.add(object);
                System.out.println(object);
            }
        }
        System.out.println(jsonArray.size());
        System.out.println(jsonArray);
        return jsonArray;
    }

    public static List<SourceParamB> mapToList(HashMap<String,SourceParamB> map){
        List<SourceParamB> list = new ArrayList<>();
        for(Map.Entry<String,SourceParamB> entry : map.entrySet()){
            list.add(entry.getValue());
        }
        return list;
    }

    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }

}
